package com.example.aijiancece;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;

import com.example.aijiancece.database.UserRepository;

public class UserInfoActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);

        MaterialToolbar toolbar = findViewById(R.id.toolbar);
        TextView tvUsername = findViewById(R.id.tvUsername);

        toolbar.setNavigationOnClickListener(v -> {
            finish();
            overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
        });

        // 显示用户名
        tvUsername.setText(UserManager.getInstance(this).getUsername());

        // 修改密码
        findViewById(R.id.layoutChangePassword).setOnClickListener(v -> showChangePasswordDialog());
    }

    private void showChangePasswordDialog() {
        View dialogView = getLayoutInflater().inflate(R.layout.dialog_change_password, null);
        TextInputEditText etOldPassword = dialogView.findViewById(R.id.etOldPassword);
        TextInputEditText etNewPassword = dialogView.findViewById(R.id.etNewPassword);
        TextInputEditText etConfirmPassword = dialogView.findViewById(R.id.etConfirmPassword);
        TextInputLayout tilOldPassword = dialogView.findViewById(R.id.tilOldPassword);
        TextInputLayout tilNewPassword = dialogView.findViewById(R.id.tilNewPassword);
        TextInputLayout tilConfirmPassword = dialogView.findViewById(R.id.tilConfirmPassword);

        MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(this)
                .setTitle("修改密码")
                .setView(dialogView)
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", null);

        AlertDialog dialog = builder.create();
        dialog.show();

        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(v -> {
            String oldPassword = etOldPassword.getText().toString().trim();
            String newPassword = etNewPassword.getText().toString().trim();
            String confirmPassword = etConfirmPassword.getText().toString().trim();

            // 重置错误提示
            tilOldPassword.setError(null);
            tilNewPassword.setError(null);
            tilConfirmPassword.setError(null);

            // 验证输入
            if (oldPassword.isEmpty()) {
                tilOldPassword.setError("请输入原密码");
                return;
            }
            if (newPassword.isEmpty()) {
                tilNewPassword.setError("请输入新密码");
                return;
            }
            if (confirmPassword.isEmpty()) {
                tilConfirmPassword.setError("请确认新密码");
                return;
            }
            if (!newPassword.equals(confirmPassword)) {
                tilConfirmPassword.setError("两次输入的密码不一致");
                return;
            }

            // 修改密码
            String username = UserManager.getInstance(this).getUsername();
            UserRepository.getInstance(this).changePassword(username, oldPassword, newPassword, new UserRepository.OnResultListener() {
                @Override
                public void onSuccess() {
                    runOnUiThread(() -> {
                        Toast.makeText(UserInfoActivity.this, "密码修改成功", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    });
                }

                @Override
                public void onError(String error) {
                    runOnUiThread(() -> {
                        tilOldPassword.setError(error);
                    });
                }
            });
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
    }
} 